package org.jeecg.modules.demo.yxadjust.service.impl;

import org.jeecg.modules.demo.soOrderDetail.entity.SoOrderDetail;
import org.jeecg.modules.demo.soOrderDetail.mapper.SoOrderDetailMapper;
import org.jeecg.modules.demo.utils.BigDecimalUtil;
import org.jeecg.modules.demo.yxadjust.entity.YxAdjust;
import org.jeecg.modules.demo.yxadjust.mapper.YxAdjustMapper;
import org.jeecg.modules.demo.yxadjust.service.IYxAdjustService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 调整单
 * @Author: jeecg-boot
 * @Date:   2022-06-09
 * @Version: V1.0
 */
@Service
public class YxAdjustServiceImpl extends ServiceImpl<YxAdjustMapper, YxAdjust> implements IYxAdjustService {
    @Resource
    private YxAdjustMapper yxAdjustMapper;
    @Resource
    private SoOrderDetailMapper soOrderDetailMapper;

    @Override
    public String getMaxCode(){
        return yxAdjustMapper.getMaxCode();
    }

    @Override
    public List<YxAdjust> getBySodNo(String str){
        return  yxAdjustMapper.getBySodNo(str);
    }


    @Override
    @Transactional
    public boolean checkAdjust(YxAdjust yxAdjust){
        try{
            yxAdjust.setAdjustTime(new Date());
            yxAdjust.setShenhe(1);
            SoOrderDetail soOrderDetail = yxAdjustMapper.getBysoNo(yxAdjust.getSodNo());
            soOrderDetail.setTzJine(new BigDecimal(BigDecimalUtil.add(soOrderDetail.getTzJine().doubleValue(),yxAdjust.getAdjustAmount().doubleValue())));
            soOrderDetail.setTzhJine(new BigDecimal(BigDecimalUtil.add(soOrderDetail.getTzhJine().doubleValue(),yxAdjust.getAdjustAmount().doubleValue())));
            soOrderDetailMapper.updateById(soOrderDetail);
            yxAdjustMapper.updateById(yxAdjust);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }
    @Override
    @Transactional
    public boolean unCheckAdjust(YxAdjust yxAdjust){
        try{
            yxAdjust.setAdjustTime(new Date());
            yxAdjust.setShenhe(0);
            SoOrderDetail soOrderDetail = yxAdjustMapper.getBysoNo(yxAdjust.getSodNo());
            soOrderDetail.setTzJine(new BigDecimal(BigDecimalUtil.sub(soOrderDetail.getTzJine().doubleValue(),yxAdjust.getAdjustAmount().doubleValue())));
            soOrderDetail.setTzhJine(new BigDecimal(BigDecimalUtil.sub(soOrderDetail.getTzhJine().doubleValue(),yxAdjust.getAdjustAmount().doubleValue())));
            soOrderDetailMapper.updateById(soOrderDetail);
            yxAdjustMapper.updateById(yxAdjust);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

}
